var regExpPassword = /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? .]).*$/,
    token = localStorage.getItem("token")
localStorage.setItem("Refresh", 1)


$(".change-password input").on("focus", function () {
    $(this).css("border-color", "#89B1FF")
    stop()
}).on("blur", function () {
    $(this).css("border-color", "#d8d8d8")
    setTimeout(function () {
    }, 300)
    move()
})

//恢复密码
$(".recovery").on("click", function (e) {
    e.preventDefault()
    let recoveryPasswordNum = Sendajax("http://119.23.46.241:8080/login/personalpage/recoverypassword", "post", "", token)
    if (recoveryPasswordNum.code == 201) {
        layer.confirm('恢复密码成功', {
            btn: ['确定']
            , btn1: function (index, layero) {
                window.history.back(-1)
                move()
            }
        });
    }
    if (recoveryPasswordNum.code == 500 && recoveryPasswordNum.data == 0) {
        layer.confirm('操作频繁，十分钟后再试', {
            btn: ['确定']
            , btn1: function (index, layero) {
                window.history.back(-1)
                move()
            }
        });
    }
})

//修改密码
$(".modify").on("click", function (e) {
    e.preventDefault()

    //判断旧密码正则表达式
    if (!regExpPassword.test($(".old-password input").val())) {
        $(".old-password input").css("border-color", "#F96B6B")
    }

    //判断新密码正则表达式
    if (!regExpPassword.test($(".new-password input").val())) {
        $(".new-password input").css("border-color", "#F96B6B")
    }

    //判断确认密码正则表达式
    if (!regExpPassword.test($(".confirm-password input").val())) {
        $(".confirm-password input").css("border-color", "#F96B6B")
        stop()
        let tip = layer.confirm('密码安全等级不够', {
            btn: ['确定']
            , btn1: function (index, layero) {
                layer.close(tip)
                move()
            }
        });
        return true
    }

    if ($(".new-password input").val() != $(".confirm-password input").val()) {
        $(".new-password input").css("border-color", "#F96B6B")
        $(".confirm-password input").css("border-color", "#F96B6B")
        stop()
        let tip = layer.confirm('密码安全等级不够', {
            btn: ['确定']
            , btn1: function (index, layero) {
                layer.close(tip)
                move()
            }
        });
        return true
    }


    if (regExpPassword.test($(".old-password input").val()) && regExpPassword.test($(".new-password input").val()) && regExpPassword.test($(".confirm-password input").val())) {
        let modifyBool = Sendajax("http://119.23.46.241:8080/login/personalpage/updatepassword", "post", {
            oldpassword: $(".old-password input").val(),
            password: $(".new-password input").val(),
            repassword: $(".confirm-password input").val()
        }, token)
        if (modifyBool.code == 500) {
            $(".old-password input").css("border-color", "#F96B6B")
            stop()
            let tip = layer.confirm('请输入正确的旧密码', {
                btn: ['确定']
                , btn1: function (index, layero) {
                    layer.close(tip)
                    move()
                }
            });
        }
        if (modifyBool.code == 201) {
            $(".old-password input").val('')
            $(".new-password input").val('')
            $(".confirm-password input").val('')
            stop()
            layer.confirm('修改密码成功,请重新登录', {
                btn: ['确定']
                , btn1: function (index, layero) {
                    window.location.href = 'userLoginPage.html'
                    move()
                }
            });
        }
    }
})

